Structure Arrays

A structure array is a MATLAB array that calls fields. The fields of a structure array can contain data of any type. For example, one field might contain a text string representing a name, another field might contain a scalar representing a category, and the third field might be a matrix of measurement results, and so on.

Creating Structure Arrays

There are two ways to generate structure arrays: one is using assignment statements, and the other is using the struct function. They are introduced separately below.

1. Using Assignment Statements

You can generate a simple 1×1 structure array by assigning data to individual fields.

[Example 2-75]

Create a student structure array as follows:

code.matlab
student.name = 'Lu dan';
student.ID = 02;
student.test = [79 75 73; 80 78 77.5; 80 85 85];

Enter in the command line:

code.matlab
student

The result shows:

code.matlab
student =
    name: 'Lu dan'
       ID: 2
     test: [3x3 double]

student is a structure array containing 3 fields. To expand the structure array, add an index number after the structure name.

code.matlab
student(2).name = 'Han xu';
student(2).ID= 10;
student(2).test = [68 70 68; 78 88 81; 92 90 93];

Now the size of the student structure array is [1,2].

Note: Once a structure array contains more than one element, entering the array name in MATLAB will not display the individual field contents, but a summary list of the information categories contained in the structure.

The fields of the 1×2 structure array student are:

code.matlab
student =
  1x2 struct array with fields:
    name
    ID
    test

You can also use the fieldnames function to get this information. This function returns a cell array containing the field names. When expanding the structure, MATLAB fills unspecified fields with empty matrices, so all structures in the array have the same field names. For example, entering:

code.matlab
student(3).name = 'Zhang ling'

Expands the size of the student array to [1,3]. Now, student(3).ID and student(3).test both contain empty matrices.

Note: It is not required that the field sizes of each element in the array be the same. In the student structure, the name field can have different lengths, the test field can be an array of any size, etc.

2. Using the struct Function

You can use the struct function to pre-allocate a structure array. Its basic form is:

code.matlab
str_array = struct('field1',val1,'field2',val2, ...)

Where the variables are the field names and their corresponding values.

Getting Data in Structure Arrays

Using structure array indexing, you can get any field value or field element in the structure array. Similarly, you can assign values to any field or field cell. Adding an index range after the structure array name can get a sub-array.

[Example 2-76]

Use the command line below to generate a 1×2 structure array.

code.matlab
mystudents = student(1:2)
mystudents =
  1x2 struct array with fields:
    name
    ID
    test

The first structure in the mystudents array is the same as the first structure in the student array.

code.matlab
mystudents(1)
ans =
    name: 'Lu dan'
       ID: 2
     test: [3x3 double]

To get a specific structure's field, add a dot after the structure name, followed by the field name. That is:

code.matlab
str = student(2).name
str =
Han xu

You can assign values, for example:

code.matlab
student(3).test(2,2) = 87;

You can extract field values from a structure one at a time. The command line below creates a 1×3 vector containing all ID fields.

code.matlab
IDs = [student.ID]
IDs =
     2    10

Similarly, you can create a cell array containing test data for the first two structures.

code.matlab
tests = {student(1:2).test}
tests =
    [3x3 double]    [3x3 double]

Size of Structure Arrays

Use the size function to get the size of a structure array or any structure field. Given a structure array name as a variable, size returns a vector containing the sizes of each dimension of the array. Given a variable of the form array(n).field, the size function returns a vector containing the size of the field content. For example: for a 1×3 structure array student, size(student) returns the vector [1,3]. The statement size(student(1,2).name) returns the length of the name string of element (1,2) of student.

Manipulating Fields

You can add a field to each structure in the array by adding a field to a single structure.

[Example 2-77] Add a gender field to the student array.

Use an assignment statement to add the following field to the structure.

code.matlab
student(2).gender = 'boy';

Now student(2).gender has the specified value. Each other structure in the array also has the gender field, but these fields contain empty matrices until they are assigned values.

You can use the rmfield function to delete a given field from each structure in the structure array. Its basic form is struc2 = rmfield(array, 'field'), where array is a structure array and "field" is the name of the field to be deleted. For example, to delete the name field from the student array, enter:

code.matlab
student = rmfield(student,'name');

You can manipulate fields and field elements just like other MATLAB functions and operators. Use indexing to get the elements to manipulate. For example, calculate the row mean of the test array in student(2):

code.matlab
mean((student(2).test));

Sometimes, you can use functions and operators on all fields in a structure array in multiple ways. The way to find the sum of all ID fields in the student array is:

code.matlab
total = 0;
for k = 1:length(student)
    total = total + student(k).ID;
end

You can simplify the operation above with the following statement:

code.matlab
total = sum([student.ID]);

This equals using a comma-separated list:

code.matlab
total = sum([student(1).ID, student(2).ID...]);

Structure Nesting

A structure field can contain another structure, or even a structure array. Once a structure is created, you can nest structures within existing structure fields using the struct function or assignment statements.

1. Creating Nested Structures Using the struct Function

To create nested structures, you can nest calls to the struct function.

[Example 2-78] Create the structure array below:

code.matlab
A = struct('data',[3 4 7; 8 0 1],'nest',...
struct('testnum','Test 1', 'xdata',[4 2 8],...
'ydata',[7 1 6]));

You can generate nested structures with assignment statements. These statements add a second element to the array. For example:

code.matlab
A(2).data = [9 3 2; 7 6 5];
A(2).nest.testnum = 'Test 2';
A(2).nest.xdata = [3 4 2];
A(2).nest.ydata = [5 0 9];

2. Indexing Nested Structures

To index nested structures, add the nested field name using the dot (.) notation. The first text string in the index expression identifies the structure array, and the following expressions access the field names containing other structures. For example, the array A created earlier has three levels of nesting. Use A(1).nest to get the nested structure in A(1). Use A(2).nest.xdata to get the xdata field of the nested structure of A(2), and use A(1).nest.ydata(2) to get the second element of the ydata field in A(1).

2. Indexing Nested Structures

To index nested structures, add the nested field name using the dot (.) notation. The first text string in the index expression identifies the structure array, and the following expressions access the field names containing other structures. For example, the array A created earlier has three levels of nesting. Use A(1).nest to get the nested structure in A(1). Use A(2).nest.xdata to get the xdata field of the nested structure of A(2), and use A(1).nest.ydata(2) to get the second element of the ydata field in A(1).